<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${tableClassModel.qualifiedClassName}">
	<!-- 定义${tableClassModel.className} 的复杂关联map -->
	<resultMap type="${beanQualifiedClassName}" id="${tableClassModel.className}Map">
		<id property="${tableClassModel.primaryKey.property}" column="${tableClassModel.primaryKey.column}" />
		<result property="createBy" column="create_by" />
		<result property="createTime" column="create_time" />
		<result property="updateBy" column="update_by" />
		<result property="updateTime" column="update_time" />
		<#list tableClassModel.mapping as item>
			<#if !item.isPrimaryKey>
			<result property="${item.property}" column="${item.column}" />
			</#if>
		</#list>	
	</resultMap>
	
	
	<!-- 定义${tableClassModel.className} 的简单map  ，本map不添加其他的关联属性 -->
	<resultMap type="${beanQualifiedClassName}" id="${tableClassModel.className}SimpleMap">
		<id property="${tableClassModel.primaryKey.property}" column="${tableClassModel.primaryKey.column}" />
		<result property="createBy" column="create_by" />
		<result property="createTime" column="create_time" />
		<result property="updateBy" column="update_by" />
		<result property="updateTime" column="update_time" />
		<#list tableClassModel.mapping as item>
			<#if !item.isPrimaryKey>
			<result property="${item.property}" column="${item.column}" />
			</#if>
		</#list>	
	</resultMap>
	
	<!-- 字段sql -->
	<sql id="columns">
		create_by,
		create_time,
		update_by,
		update_time,
		<#list tableClassModel.mapping as item> 
			<#if item_has_next>
			${item.column},
			<#else>
			${item.column}
			</#if>
		</#list>
	</sql>
	
	<!-- 属性sql -->
	<sql id="propertys">
		${'#'}{item.createBy},
		now(),
		${'#'}{item.updateBy},
		now(),
		<#list tableClassModel.mapping as propertyitem>
		<#if propertyitem_has_next>
			${'#'}{item.${propertyitem.property}},
			<#else>
			${'#'}{item.${propertyitem.property}}
			</#if>
		</#list>	
	</sql>
	
	<!-- where sql -->
	<sql id="where_sql">
		 
		 <if test="record!=null">
			<#list tableClassModel.mapping as item> 
			<if test="(record.${item.property}!=null and record.${item.property}!='') or  (record.${item.property}!='' and record.${item.property}==0)  ">
				and ${item.column}  = ${'#'}{record.${item.property}} 
			</if>
			</#list>	
		</if>
		 
	</sql>
	
	<!--  插入方法   -->
	<insert id="insert" parameterType="${beanQualifiedClassName}"
		useGeneratedKeys="true" keyProperty="item.${tableClassModel.primaryKey.property}">
		INSERT INTO ${tableClassModel.tableName} (
			 <include refid="columns"></include>
		)
	VALUES (
		 <include refid="propertys"></include>
	)
	</insert>
	
	
	
	<!--  批量插入   -->
	<insert id="batchInsert" parameterType="java.util.List">
		INSERT INTO ${tableClassModel.tableName} (
		<include refid="columns"></include>	
		)
	VALUES 
	<foreach collection="list" item="item" index="index" separator=",">(
		<include refid="propertys"></include>	
	)</foreach>
	</insert>
	
	
	
	
	
	<!--  根据Map更新 部分更新   -->
	<update id="updateByMap" parameterType="java.util.HashMap" >
		UPDATE ${tableClassModel.tableName}
		<set>
			<#list tableClassModel.mapping as item>
			<#if !item.isPrimaryKey>
			<if test="_parameter.containsKey('${item.property}')">
					${item.column} = ${'#'}{${item.property}},
				</if>		
			</#if>
			</#list>
		</set>
		WHERE ${tableClassModel.primaryKey.column}=${'#'}{${tableClassModel.primaryKey.property}} 
	</update> 
	
	
	<!--  根据对象更新 部分更新   -->
	<update id="updateByModel" parameterType="${tableClassModel.primaryKey.classType}">
		UPDATE ${tableClassModel.tableName}
		<set>
			<#list tableClassModel.mapping as item>
			<#if !item.isPrimaryKey>
			<#if item.classType = "String">
			<if test="record.${item.property} != null and record.${item.property} != '' ">
				${item.column}  = ${'#'}{record.${item.property}}, 
			</if>
			<#else>
			<if test="record.${item.property} != null ">
				${item.column}  = ${'#'}{record.${item.property}}, 
			</if>
			</#if>	
			</#if>
			</#list>
		</set>
		WHERE ${tableClassModel.primaryKey.column}=${'#'}{record.${tableClassModel.primaryKey.property}} 
	</update>
	
	<!-- 批量删除 -->
	<delete id="deleteByIds" parameterType="java.util.List">
		delete from ${tableClassModel.tableName} where  ${tableClassModel.primaryKey.column} in
		<foreach collection="list" index="index" item="item" open="("
			separator="," close=")">
			${'#'}{item}
		</foreach>
	</delete>
		
	<!-- 根据id删除-->
	<delete id="deleteById" parameterType="${tableClassModel.primaryKey.classType}">
		DELETE FROM ${tableClassModel.tableName}
		where  ${tableClassModel.primaryKey.column}=${'#'}{${tableClassModel.primaryKey.property}} 
	</delete>
	
	<!-- 根据对象删除-->
	<delete id="deleteByModel" parameterType="${beanQualifiedClassName}">
		DELETE FROM ${tableClassModel.tableName}
		<where>
		<include refid="where_sql" ></include>
		</where>
	</delete>
	
	
	
	<!-- 分页查询 -->
	<select id="selectInPage" resultMap="${tableClassModel.className}Map">
		select 
		<include refid="columns" ></include>
		from ${tableClassModel.tableName}
		<where>
		  <include refid="where_sql"></include>
		 </where>
		<if test="pageVo !=null"><!-- 判断pageVo对象是否为空 -->
			<if test="pageVo.sort !=null  and pageVo.order !=null">
				order by
				${'$'}{pageVo.sort} ${'$'}{pageVo.order}
			</if>
			<if test="pageVo.offset >=0  and pageVo.limit >0">
				limit
				${'#'}{pageVo.offset},${'#'}{pageVo.limit}
			</if>
		</if>
	</select>
	
	<!-- 查询总条数 -->
	<select id="selectTotalRecord" parameterType="long"   resultType="java.lang.Integer">
	select count(*)
	from ${tableClassModel.tableName}
		<where>
		   <include refid="where_sql"></include>
		</where>
	</select>

	<!-- 根据id查询-->
	<select id="selectById" resultMap="${tableClassModel.className}Map">
		select 
		<include refid="columns" ></include>
		from ${tableClassModel.tableName}
		where  ${tableClassModel.primaryKey.column}=${'#'}{${tableClassModel.primaryKey.property}} 
	</select>	
	
	
	<!-- 根据id 锁表查询-->
	<select id="selectForUpdate" resultMap="${tableClassModel.className}Map">
		select 
		<include refid="columns" ></include>
		from ${tableClassModel.tableName}
		where  ${tableClassModel.primaryKey.column}=${'#'}{${tableClassModel.primaryKey.column}} 
		for update
	</select>	
	
	
	
	<!-- 根据对象查询-->
	<select id="selectByModel" resultMap="${tableClassModel.className}Map">
		select 
		<include refid="columns" ></include>
		from ${tableClassModel.tableName}
		<where>
		  <include refid="where_sql"></include>
		 </where>
	</select>
</mapper>